﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;
using Entity;
using Business; 

namespace WebApp
{
    public partial class ProvinceforAdmin : System.Web.UI.Page
    {
        SqlConnection con;
        DataSet ds;
        SqlCommand cmd;
        SqlDataAdapter da;
        SqlDataReader dr;
        string strCon;

        protected void Page_Load(object sender, EventArgs e)
        {
            strCon = "Data Source = (local); Database =ProjectSem3; Trusted_Connection = true";
            con = new SqlConnection(strCon);
            ds = new DataSet();
            if (!IsPostBack)
                Bindnews();
        }
        private void Bindnews()
        {

            GridView1.DataSource = GridDataSource();
            GridView1.DataBind();
        }
        private DataTable GridDataSource()
        {
            Province_Bussiness pb = new Province_Bussiness();
            DataTable dt = new DataTable();
            try
            {
                dt = pb.LoadP();
            }
            catch (Exception ee)
            {
                throw new Exception(ee.Message);
            }
            finally
            {
                pb = null;
            }

            return dt;
        }

        protected void Add_news(object sender, EventArgs e)
        {
           
        }

        protected void btnclear_Click(object sender, EventArgs e)
        {
            txtName.Text = "";
            txtPrice.Text = "";
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Bindnews();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Bindnews();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Province_Bussiness prB = new Province_Bussiness();
            GridViewRow row = GridView1.Rows[e.RowIndex];
            int provinceID = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text);
            int getRowCount = 0;
            string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            int price = Convert.ToInt32(((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text);
            getRowCount = prB.Update(provinceID, name, price);
            GridView1.EditIndex = -1;
            Bindnews();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
            Province_Bussiness prBu = new Province_Bussiness();
            try
            {
                prBu.Delete(id);

                Label2.Text = "Record Deleted Successfully.";
            }
            catch (Exception ee)
            {
                Label2.Text = ee.Message.ToString();
            }
            finally
            {
                prBu = null;
            }

            GridView1.EditIndex = -1;
            // Refresh the list
            Bindnews();
        }

        protected void btninsert_Click(object sender, EventArgs e)
        {
            Province_Entity pr = new Province_Entity();
            Province_Bussiness Insert = new Province_Bussiness();

            pr.Provincename = txtName.Text.Trim();
            pr.Price = Convert.ToInt32(txtPrice.Text.Trim());


            Insert.Add_Province(pr);
            Bindnews();


            txtName.Text = "";
            txtPrice.Text = "";
        }

        protected void btnsearch_Click(object sender, EventArgs e)
        {
            try 
            {
                if (con.State == ConnectionState.Closed)
                    con.Open();
                string q = "select * from province where provinceName = '" + txtSearch.Text +"'";
                cmd = new SqlCommand(q, con);
                cmd.CommandType = CommandType.Text;
                da = new SqlDataAdapter(cmd);
                da.Fill(ds, "province");
                GridView1.DataSource = ds.Tables["province"];
                GridView1.DataBind();
            }
            catch (SqlException ex) 
            {
                throw new Exception(ex.Message);
            }
            finally 
            {
                con.Close();
            }
        }

    }
}